<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/interfaces/IProductAccessDao.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/interfaces/IConnectionAccessDao.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/factory/ConnectionFactory.php';

	error_reporting (E_ALL ^ E_NOTICE);
	
	$debug = $_SESSION['debug'];

	class MySqlProductAccessDao implements IProductAccessDao {  
		/**
		 * Constructor of the product dao
		 */
	 	public function __construct () {}
  
  		/**
		 * Creates a new product
		 */
     	public function createProduct ($pkCode, $name, $unit_cost)  {
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
			
			$query = "INSERT INTO PRODUCT
					   VALUES ('$name', '$unit_cost')";
		 
			$sentence = mysql_query($query, $conn);
			
			if ($sentence)
				return true;
			
			return false;
		 
		 }
	 	
		 /**
		  * Updates an existing product
		  */		
		 public function changeProduct  ($pkCode, $name, $unit_cost) 
		 {
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
			
			$query = "UPDATE PRODUCT SET $name='$name', $unit_cost=$unit_cost";
			
			$sentence = mysql_query($query,$conn);
			
			if ($sentence)
				return true;
			
			return false;
		 }
		 
		 /**
		  * Selects all products
		  */
		 public function selectProducts() {
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
			
			$query = "SELECT pk_code, name, specifications FROM PRODUCT";
			
			$sentence = mysql_query($query, $conn);
				
			$conn = $connection -> closeConnection($conn);
			
			return $sentence;
		 }
		 
		 /**
		  * Select single product by code
		  */
		 public function selectProductByCode($pkCode) {
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
			
			$query = "SELECT name, unit_cost, unit_cost_currency, product_units 
					  FROM PRODUCT
					  WHERE pk_code = $pkCode";
					  
			$GLOBALS['debug'] -> log($query, "Query");
			
			$sentence = mysql_query($query, $conn);
				
			$conn = $connection -> closeConnection($conn);
			
			return $sentence; 
		 }
  	}
?>